﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iShoppingBUS;

namespace iShopping.Admin.UserControl
{
    public partial class CapNhapQuyen : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillLoaiTaiKhoan();
                FillData();
                FillQuyen();
                
            }

        }

        public  void FillData()
        {
            DataTable dt = LoaiTaiKhoanBUS.SelectAllLoai();
            ddlLoaiTaiKhoan.DataSource = dt;
            ddlLoaiTaiKhoan.DataTextField = "TenLoaiTK";
            ddlLoaiTaiKhoan.DataValueField = "MaLoaiTK";
            ddlLoaiTaiKhoan.DataBind();

            DataTable dataTable = PhanQuyenBUS.SelectAllQuyen();
            ddlQuyen.DataSource = dataTable;
            ddlQuyen.DataValueField = "MaPhanQuyen";
            ddlQuyen.DataTextField = "MaPhanQuyen";
            ddlQuyen.DataBind();
        }
        public void FillLoaiTaiKhoan()
        {
            //Giữ lại giá trị cũ của cột Loai
            //String tenLoai = ddlLoai.SelectedItem.Text.ToString();
            DataTable dt = LoaiTaiKhoanBUS.SelectAllLoai();
            ddlLoai.DataSource = dt;
            ddlLoai.DataTextField = "TenLoaiTK";
            ddlLoai.DataValueField = "MaLoaiTK";
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    if (dt.Rows[i]["TenLoaiTK"].ToString() == tenLoai)
            //    {
            //        ddlLoai.SelectedIndex = i;
            //        break;
            //    }
            //}
            ddlLoai.DataBind();
        }
        private void FillQuyen()
        {
            string maLoai = ddlLoai.SelectedItem.Value.ToString();
            DataTable dt = LoaiTaiKhoanBUS.SelectOfMa(maLoai);
            GRPhanQuyen.DataSource = dt;
            GRPhanQuyen.DataBind();
        }

        protected void GRPhanQuyen_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // phan trang
            GRPhanQuyen.PageIndex = e.NewPageIndex;
            FillQuyen();
        }

        protected void GRPhanQuyen_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GRPhanQuyen.EditIndex = -1;
            FillQuyen();
        }

        protected void GRPhanQuyen_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //  xóa quyen
            if (GRPhanQuyen.DataKeys != null)
            {
                string maLoaiTaiKhoan = GRPhanQuyen.DataKeys[e.RowIndex].Value.ToString();
                Label lblMaQuyen = (Label)GRPhanQuyen.Rows[e.RowIndex].Cells[2].FindControl("lblMaQuyen");
                string maQuyen = lblMaQuyen.Text.Trim();
                bool result = ChiTietPhanQuyenBUS.DeleteChiTietPhanQuyen(maLoaiTaiKhoan, maQuyen);
                if (result)
                {
                    FillQuyen();
                }
            }
        }

        private string maQuyens;
        protected void GRPhanQuyen_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //Giữ lại giá trị cũ của cột Loai
            String maPhanQuyen = ((Label)GRPhanQuyen.Rows[e.NewEditIndex].Cells[2].FindControl("lblMaQuyen")).Text;

            ViewState["MaQuyen"] = maPhanQuyen;
            // Chuyen sang che do Edit
            GRPhanQuyen.EditIndex = e.NewEditIndex;
            FillQuyen();

            string maLoaiTaiKhoan = GRPhanQuyen.DataKeys[e.NewEditIndex].Value.ToString();
            //Thực hiện đỗ dữ liệu vào DropDownList loai
            DropDownList dropDownListMa = (DropDownList)GRPhanQuyen.Rows[e.NewEditIndex].FindControl("ddlMaQuyen");
            DataTable dt = LoaiTaiKhoanBUS.SelectAll();
            dropDownListMa.DataSource = dt;
            dropDownListMa.DataTextField = "TenQuyen";
            dropDownListMa.DataValueField = "MaPhanQuyen";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["MaPhanQuyen"].ToString() == maPhanQuyen)
                {
                    dropDownListMa.SelectedIndex = i;
                    break;
                }
            }
            dropDownListMa.DataBind();



        }

        protected void GRPhanQuyen_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            GRPhanQuyen.EditIndex = -1;
            FillQuyen();
        }

        protected void GRPhanQuyen_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string maLoai = GRPhanQuyen.DataKeys[GRPhanQuyen.EditIndex].Value.ToString();
            maQuyens = ViewState["MaQuyen"].ToString();
            if (e.NewValues == e.OldValues)
                e.Cancel = true;
            else
            {
                string maChiTietQuyen = ChiTietPhanQuyenBUS.SelectMaChiTiet(maLoai, maQuyens);
                GridView gv = GRPhanQuyen;
                DropDownList ddlMaQuyen = (DropDownList)gv.Rows[gv.EditIndex].FindControl("ddlMaQuyen");

                if (gv.DataKeys != null)
                {
                    int idChiTiet = 0;
                    Int32.TryParse(maChiTietQuyen, out idChiTiet);
                    string maQuyen = ddlMaQuyen.SelectedItem.Value.Trim();
                    bool result = ChiTietPhanQuyenBUS.UpdateChiTietQuyen(idChiTiet, maQuyen);
                    if (result)
                    {
                        GRPhanQuyen.EditIndex = -1;
                        FillQuyen();
                    }
                }
            }

        }


        protected void ddlLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            string maLoaiTK = ddlLoai.SelectedItem.Value.Trim();
            DataTable dt = LoaiTaiKhoanBUS.SelectOfMa(maLoaiTK);
            GRPhanQuyen.DataSource = dt;
            GRPhanQuyen.DataBind();
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            string maLoai = ddlLoaiTaiKhoan.SelectedItem.Value.Trim();
            string maQuyen = ddlQuyen.SelectedItem.Value.Trim();
            
            bool result = ChiTietPhanQuyenBUS.InsertChiTiet(maLoai,maQuyen);
            if(result)
            {
                FillQuyen();
            }
        }
    }
}